package com.lipuwater.agent.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.lipuwater.agent.R;
import com.lipuwater.agent.utils.preferences.PreferenceKey;
import com.lipuwater.agent.utils.preferences.PreferencesUtils;


public class AppUpdataDialog extends Dialog implements
        View.OnClickListener {
    private DialogClickListener listener;
    private Button dialog_sure, dialog_cancel;
    private TextView update_content;
    private String updateContent;
    private CheckBox mCheckBox;
    private boolean force = false;
    private int versionCode = 0;

    /**
     * 自定义dialog
     *
     * @param context
     * @param content  主体文字
     * @param listener 回调接口
     */
    public AppUpdataDialog(Context context, boolean force, int versionCode, String content,
                           DialogClickListener listener) {
        super(context, R.style.MyDialogStyle);
        this.force = force;
        this.versionCode = versionCode;
        this.updateContent = content;
        this.listener = listener;
    }


    ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_app_updata);
        dialog_sure = (Button) findViewById(R.id.dialog_sure);
        dialog_cancel = (Button) findViewById(R.id.dialog_cancel);
        dialog_sure.setOnClickListener(this);
        dialog_cancel.setOnClickListener(this);
        update_content = (TextView) findViewById(R.id.update_content);
        mCheckBox = (CheckBox) findViewById(R.id.CheckBox);
        if (force) {
            mCheckBox.setVisibility(View.GONE);
            //  mCheckBox.setClickable(false);
            dialog_cancel.setText("退出应用");
        }
        initView();
        initDialog();
    }

    /**
     * 设置dialog的宽为屏幕的3分之1
     */
    private void initDialog() {
        setCanceledOnTouchOutside(false);
        setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getRepeatCount() == 0) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        WindowManager windowManager = this.getWindow().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = (int) (display.getWidth() / 6 * 4.5); // // 设置宽度
        this.getWindow().setAttributes(lp);
    }

    private void initView() {
        update_content.setText(updateContent);
    }

    public interface DialogClickListener {
        void onLeftBtnClick(Dialog dialog);

        void onRightBtnClick(Dialog dialog);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.dialog_sure) {
            if (listener != null) {
                listener.onRightBtnClick(this);
            }
        } else if (id == R.id.dialog_cancel) {
            if (mCheckBox.isChecked()) {
                //用戶忽略改版本
                PreferencesUtils.getInstance().setParam(PreferenceKey.IGNORE_VERSION, versionCode);
            }
            if (listener != null) {
                listener.onLeftBtnClick(this);
            }
        }
    }
}